package com.jhgsys.internal.base.factory;

import com.jhgsys.internal.base.entity.BaseNotice;
import com.jhgsys.internal.base.factory.enums.NoticeObjectEnums;
import com.jhgsys.internal.base.service.IBaseNoticeService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 *
 * @author 冯海军
 */
@Slf4j
@Component
public class NoticeFactory {




    /**
     * @param baseNoticeList 通知信息
     */
    public static TimerTask saveNotice(List<BaseNotice> baseNoticeList,String userName) {
//        AsyncManager.me().execute(NoticeFactory.saveNotice(baseNoticeList,""));
        return new TimerTask() {
            @Override
            public void run() {
                try{
                    String createdBy = StringUtils.isBlank(userName)? "system" : userName;
                    Date date = new Date();
                    if(null != baseNoticeList && baseNoticeList.size() > 0){
                        for (BaseNotice baseNotice : baseNoticeList) {
                            NoticeObjectEnums noticeObjectByValue = NoticeObjectEnums.getNoticeObjectByValue(baseNotice.getNoticeObject());
                            String noticeTitle= "";
                            switch (noticeObjectByValue){
                                case ACCIDENT_SURVEY_ASSIGNMENT:
                                    noticeTitle = "【"+noticeObjectByValue.getLabel()+"】有新的事故上报需您去调查，请查收！";
                                    break;
                                case ACCIDENT_RECTIFICATION_ASSIGNMENT:
                                    noticeTitle = "【"+NoticeObjectEnums.ACCIDENT_SURVEY_ASSIGNMENT.getLabel()+"】有新的事故上报需要您去整改，请查收！";
                                    break;
                                default:break;
                            }
                            baseNotice.setNoticeTitle(noticeTitle);
                            baseNotice.setNoticeContent(noticeTitle);
                            baseNotice.setSendDate(date);
                            baseNotice.setCreatedBy(createdBy);
                            baseNotice.setCreatedTime(date);
                            baseNotice.setStatus("0");
                            baseNotice.setDeletedFlg(FebsConstant.VALID);
                            IBaseNoticeService baseNoticeService = SpringContextUtil.getBean(IBaseNoticeService.class);
                            baseNoticeService.createBaseNotice(baseNotice);
                        }
                    }
                }catch (Exception ex) {
                    log.error("业务通知信息保存异常：错误 - " + ex.getMessage());
                }
            }
        };
    }
}
